package com.flex.mobile.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.impl.CustInfoDao;
import com.flex.mobile.dao.impl.DriverInfoDao;
import com.flex.mobile.dao.impl.UserStatusDao;
import com.flex.mobile.dao.model.CustInfo;
import com.flex.mobile.dao.model.DriverInfo;
import com.flex.mobile.dao.model.UserStatus;
import com.flex.mobile.model.User;
import com.flex.mobile.service.LoginService;

import flex.messaging.FlexContext;

@Service("loginService")
@RemotingDestination(channels = { "my-amf" })
public class LoginServiceImpl implements LoginService {
	private CustInfoDao custInfoDao;
	private DriverInfoDao driverInfoDao;
	private UserStatusDao userStatusDao;

	@Autowired
	public void setUserStatusDao(UserStatusDao userStatusDao) {
		this.userStatusDao = userStatusDao;
	}

	@Autowired
	public void setCustInfoDao(CustInfoDao custInfoDao) {
		this.custInfoDao = custInfoDao;
	}

	@Autowired
	public void setDriverInfoDao(DriverInfoDao driverInfoDao) {
		this.driverInfoDao = driverInfoDao;
	}

	@Override
	public User registerUser(User user) {
		if (User.USER_TYPE_DRIVER == user.getType()) {
			DriverInfo driverInfo = new DriverInfo();
			driverInfo.setUserName(user.getUserName());
			driverInfo.setTelephone(user.getPhone());
			driverInfo.setPassword(user.getPassword());
			driverInfo.setCarId(user.getCarId());
			driverInfo.setCompany("abc");
			driverInfo = driverInfoDao.create(driverInfo);
			user.setId(driverInfo.getId());

		} else if (User.USER_TYPE_CUST == user.getType()) {
			CustInfo custInfo = new CustInfo();
			custInfo.setUserName(user.getUserName());
			custInfo.setName(user.getName());
			custInfo.setCellPhone1(user.getPhone());
			custInfo.setPassword(user.getPassword());
			custInfo = custInfoDao.create(custInfo);
			user.setId(custInfo.getId());
		}
		return user;
	}

	@Override
	public User login(String userName, String pwd, Map<String, String> param) {
	   User user = null;
		if (User.USER_TYPE_DRIVER == Integer.parseInt(param.get("userType"))) {
			List<DriverInfo> drivers = driverInfoDao.findByNameOrPhone(userName);
			if (drivers == null || drivers.size() == 0) {
				throw new RuntimeException("用户不存在");
			}
			for (DriverInfo info : drivers) {
				if (info.getPassword().equals(pwd)) {
				   user = new User();
				   user.setUserName(info.getUserName());
				   user.setPassword(info.getPassword());
				   user.setType(User.USER_TYPE_DRIVER);
					user.setId(info.getId());
					user.setTid(info.getTid());
					user.setPhone(info.getTelephone());
					setUserStatus(user);
					return user;
				}
			}
		} else if (User.USER_TYPE_CUST == Integer.parseInt(param.get("userType"))) {
			List<CustInfo> users = custInfoDao.findByNameOrPhone(userName);
			if (users == null || users.size() == 0) {
				throw new RuntimeException("用户不存在");
			}
			for (CustInfo info : users) {
				if (info.getPassword().equals(pwd)) {
				   user = new User();
               user.setUserName(info.getUserName());
               user.setPassword(info.getPassword());
					user.setId(info.getId());
					user.setTid(info.getTid());
					user.setType(User.USER_TYPE_DRIVER);
					user.setEmail(info.getEmail());
					user.setPhone(info.getCellPhone1());
					setUserStatus(user);
					return user;
				}
			}
		}
		throw new RuntimeException("密码错误");
	}
	
	// 密码校验成功，然后判断user_status是否已经登录，如果已经登录，抛出异常，
	private void setUserStatus(User user) {
      UserStatus userStatus = userStatusDao.findByTId(user.getTid());
      if(null!=userStatus){
//         if(userStatus.getCurrentStatus() == UserStatus.CURRENT_STATUS_ONLINE){
//            throw new RuntimeException("该用户已经登录");
//         }else{
            userStatus.setCurrentStatus(UserStatus.CURRENT_STATUS_ONLINE);
            userStatus.setLoginTimes(userStatus.getLoginTimes()+1);
            userStatusDao.update(userStatus);
//         }
      }else {
         userStatus=new UserStatus();
         userStatus.setTid(user.getTid());
         userStatus.setCurrentStatus(UserStatus.CURRENT_STATUS_ONLINE);
         userStatus.setUserType(user.getType());
         userStatus.setLoginTimes(1);
         userStatusDao.create(userStatus);
      }
      
      HttpServletRequest request = FlexContext.getHttpRequest();
      HttpSession session = request.getSession();
      session.setAttribute("user", user);
	}

}
